package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Category;
import com.itheima.entity.Setmeal;
import com.itheima.entity.SetmealDish;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("套餐信息{}", setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐执行成功");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //分页构造器对象
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Setmeal::getName, name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo, lqw);

        //创建新分页构造器拷贝旧分页构造器
        Page<SetmealDto> pageInfo2 = new Page<>();
        BeanUtils.copyProperties(pageInfo,pageInfo2,"records");
        //list集合用于接收处理好的数据
        List<SetmealDto> list = new ArrayList<>();
        //获得旧分页构造器的records数据
        List<Setmeal> records = pageInfo.getRecords();
        //循环records数据，将其中的每一个CategoryName赋上值
        records.forEach(e->{
            Category category = categoryService.getById(e.getCategoryId());
            if(category!=null){
                String name1 = category.getName();
                SetmealDto setmealDto = new SetmealDto();
                //对象拷贝
                BeanUtils.copyProperties(e,setmealDto);
                setmealDto.setCategoryName(name1);
                //将赋好值的对象交给list集合
                list.add(setmealDto);
            }
        });
        //给新分页构造器赋值，值就是处理好的list集合
        pageInfo2.setRecords(list);
        return R.success(pageInfo2);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }

    /**
     * 套餐停售
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R<String> storp(@RequestParam List<Long> ids){
        log.info("ids------------------------{}",ids);
        ids.forEach(e->{
            Setmeal setmeal = setmealService.getById(e);
            setmeal.setStatus(0);
            setmealService.updateById(setmeal);
        });
        return R.success("修改状态成功");
    }

    /**
     * 套餐起售
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    public R<String> use(@RequestParam List<Long> ids){
        log.info("ids------------------------{}",ids);
        ids.forEach(e->{
            Setmeal setmeal = setmealService.getById(e);
            setmeal.setStatus(1);
            setmealService.updateById(setmeal);
        });
        return R.success("修改状态成功");
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealDto(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }

    /**
     * 根据分类id查询套餐列表
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(Setmeal::getStatus,setmeal.getStatus());
        lqw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setmealService.list(lqw);

        return R.success(setmealList);
    }


}

